layui.use(['form','layer','table'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        table = layui.table;

    //文章列表
    var tableIns = table.render({
        elem: '#linkList',
        url : 'list',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 5,
        limits : [10,15,20,25],
        id : "linkListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'id', title: 'ID', width:60, align:"center"},
            {field: 'webname', title: '网站名称', width:250},
            {field: 'webicon', title: '网站LOGO', align:"center",templet:function(d){
                    return '<img style="width: 32px;height: 32px;" src="/upload/link/'+d.webicon+'"  />';
                }},
            {field: 'url', title: '链接地址', align:'left', templet:function(d){
                    return '<a href="'+d.url+'" target="_blank">'+d.url+'</a>'
                }},
            {field: 'webmaster', title: '站长', align:'center'},
            {field: 'phone', title: '电话', align:'center'},
            {field: 'email', title: '邮箱', align:'center'},
            {field: 'status', title: '状态', align:'center'},
            {title: '操作', width:250, templet:'#linkListBar',fixed:"right",align:"center"}
        ]]
    });

   
    //添加文章
    function addLink(){
        var index = layui.layer.open({
            title : "添加友链",
            type : 2,
            shadeClose: false,           //弹出框之外的地方是否可以点击
            area : ["400px","500px"],
            content : "linkAdd",
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
            }
        })
    }


    $(".addLink_btn").click(function(){
        addLink();
    })
    function changeStsLink(data,sts){
        var id = data.id;
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        $.ajax({
            type: "post",
            url:"status",
            dataType: "json",
            data: {
                id:id,
                status:sts
            },
            success: function (result) {
                if(result.flag == "success"){
                    top.layer.msg("操作成功！");
                    tableIns.reload();
                    layer.close(index);
                }else{
                    layer.msg("操作失败", {icon: 2});
                }
            }
        });
    }

    //列表操作
    table.on('tool(linkList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'agree'){ //编辑
            changeStsLink(data,1);
        } else if(layEvent === 'disagree'){
            changeStsLink(data,2);
        }else if(layEvent === 'del'){
            layer.confirm('确定删除此友链吗？',{icon:3, title:'提示信息'},function(index){
                $.post("delete",{
                    id : data.id  //将需要删除的newsId作为参数传入
                },function(data){
                    data = JSON.parse(data)
                    if(data.flag ==="success"){
                        layer.msg("删除成功");
                        tableIns.reload();
                        layer.close(index);
                    }else if(data.flag === "permissions"){
                        layer.msg("权限不足，无法删除！",{icon: 5});
                    }else{
                        layer.msg("删除失败！", {icon: 2});
                    }
                })
            });
        }
    });
})